using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using AForge.Controls;
using AForge.Video.DirectShow;

namespace ICP_MS.Plugin.Advance;

public class FormVideo : Form
{
	private VideoCaptureDevice captureAForge = null;

	private FilterInfoCollection videoDevices;

	private IContainer components = null;

	private GroupBox groupBox3;

	private VideoSourcePlayer videoSourcePlayer1;

	private ComboBox comboBox1;

	private Button button1;

	public FormVideo()
	{
		InitializeComponent();
	}

	private void FormCameraCapture_Load(object sender, EventArgs e)
	{
		videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
		if (videoDevices != null && videoDevices.Count != 0)
		{
			foreach (FilterInfo device in videoDevices)
			{
				comboBox1.Items.Add(device.Name);
			}
		}
		else
		{
			MessageBox.Show(Lang.Txt("FormMonitor.TipsText1"));
		}
		ApplyResources();
	}

	private void ApplyResources()
	{
		groupBox3.Text = Lang.Txt("FormMonitor.VideoWindow");
	}

	private void Form1_FormClosing(object sender, FormClosingEventArgs e)
	{
		if (videoSourcePlayer1 != null || videoSourcePlayer1.IsRunning)
		{
			ShutCamera();
		}
	}

	private void videoSourcePlayer1_Click(object sender, EventArgs e)
	{
		try
		{
			captureAForge.DisplayPropertyPage(IntPtr.Zero);
		}
		catch
		{
		}
	}

	public void ShutCamera()
	{
		if (videoSourcePlayer1.VideoSource != null)
		{
			videoSourcePlayer1.SignalToStop();
			videoSourcePlayer1.WaitForStop();
			videoSourcePlayer1.VideoSource = null;
		}
	}

	private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
	{
		try
		{
			ShutCamera();
			captureAForge = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
			videoSourcePlayer1.VideoSource = captureAForge;
			videoSourcePlayer1.SignalToStop();
			videoSourcePlayer1.WaitForStop();
			videoSourcePlayer1.Start();
		}
		catch (Exception)
		{
		}
	}

	private void button1_Click(object sender, EventArgs e)
	{
		videoSourcePlayer1.Stop();
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
		this.groupBox3 = new System.Windows.Forms.GroupBox();
		this.videoSourcePlayer1 = new AForge.Controls.VideoSourcePlayer();
		this.comboBox1 = new System.Windows.Forms.ComboBox();
		this.button1 = new System.Windows.Forms.Button();
		this.groupBox3.SuspendLayout();
		base.SuspendLayout();
		this.groupBox3.Controls.Add(this.videoSourcePlayer1);
		this.groupBox3.Location = new System.Drawing.Point(12, 62);
		this.groupBox3.Name = "groupBox3";
		this.groupBox3.Size = new System.Drawing.Size(712, 588);
		this.groupBox3.TabIndex = 14;
		this.groupBox3.TabStop = false;
		this.groupBox3.Text = "视频窗口";
		this.videoSourcePlayer1.Dock = System.Windows.Forms.DockStyle.Fill;
		this.videoSourcePlayer1.Location = new System.Drawing.Point(3, 21);
		this.videoSourcePlayer1.Name = "videoSourcePlayer1";
		this.videoSourcePlayer1.Size = new System.Drawing.Size(706, 564);
		this.videoSourcePlayer1.TabIndex = 5;
		this.videoSourcePlayer1.Text = "videoSourcePlayer1";
		this.videoSourcePlayer1.VideoSource = null;
		this.videoSourcePlayer1.Click += new System.EventHandler(videoSourcePlayer1_Click);
		this.comboBox1.FormattingEnabled = true;
		this.comboBox1.Location = new System.Drawing.Point(30, 23);
		this.comboBox1.Name = "comboBox1";
		this.comboBox1.Size = new System.Drawing.Size(185, 23);
		this.comboBox1.TabIndex = 15;
		this.comboBox1.SelectedIndexChanged += new System.EventHandler(comboBox1_SelectedIndexChanged);
		this.button1.Location = new System.Drawing.Point(299, 17);
		this.button1.Name = "button1";
		this.button1.Size = new System.Drawing.Size(80, 33);
		this.button1.TabIndex = 16;
		this.button1.Text = "Stop";
		this.button1.UseVisualStyleBackColor = true;
		this.button1.Click += new System.EventHandler(button1_Click);
		base.AutoScaleDimensions = new System.Drawing.SizeF(8f, 15f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.ClientSize = new System.Drawing.Size(853, 681);
		base.Controls.Add(this.button1);
		base.Controls.Add(this.comboBox1);
		base.Controls.Add(this.groupBox3);
		base.Name = "FormVideo";
		this.Text = "等离子体监控";
		base.FormClosing += new System.Windows.Forms.FormClosingEventHandler(Form1_FormClosing);
		base.Load += new System.EventHandler(FormCameraCapture_Load);
		this.groupBox3.ResumeLayout(false);
		base.ResumeLayout(false);
	}
}
